package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.qo.CustomerReportQuery;
import cn.wolfcode.crm.service.ICustomerReportService;
import cn.wolfcode.crm.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;

    //潜在客户报表
    @RequestMapping("/list")
    @RequiresPermissions(value = {"customerReport:list", "潜在客户报表"},logical = Logical.OR)
    public String potentialList(Model model,@ModelAttribute("qo")CustomerReportQuery qo){
        PageInfo pageInfo = customerReportService.query(qo);
        model.addAttribute("result",pageInfo);
        return "/customerReport/list";
    }

    //柱状图
    @RequestMapping("/chartByBar")
    public String chartByBar(Model model, @ModelAttribute("qo")CustomerReportQuery qo){
        List<Map> list = customerReportService.selectAll(qo);
        //创建存储x,y数据的容器
        List xList = new ArrayList();
        List yList = new ArrayList();
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        //把数据共享到页面
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "/customerReport/listByBar";
    }
    //饼状图
    @RequestMapping("/chartByPie")
    public String chartByPie(Model model, @ModelAttribute("qo")CustomerReportQuery qo){
        List<Map> list = customerReportService.selectAll(qo);
        //创建存储x,y数据的容器
        List xList = new ArrayList();
        List yList = new ArrayList();
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap hashMap = new HashMap();
            hashMap.put("name",map.get("groupType"));
            hashMap.put("value",map.get("number"));
            yList.add(hashMap);
        }
        //把数据共享到页面
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "/customerReport/listByPie";
    }

}
